package com.jd.webapp.portal.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jd.webapp.portal.pojo.CartItem;
import com.jd.webapp.portal.pojo.OrderExt;
import com.jd.webapp.portal.service.CartService;
import com.jd.webapp.portal.service.OrderService;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private CartService cartService;
	@Autowired
	private OrderService orderService;

	@RequestMapping("/order-cart")
	public String showOrderCart(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CartItem> cartItemList = new ArrayList<CartItem>();
		try {
			cartItemList = cartService.getList(request);
		} catch (Exception e) {
			e.printStackTrace();
		}
		model.addAttribute("cartList", cartItemList);
		return "order-cart";
	}
	
	@RequestMapping("/create")
	public String createOrder(OrderExt order, Model model) {
		String orderId = orderService.createOrder(order);
		model.addAttribute("orderId", orderId);
		model.addAttribute("payment", order.getPayment());
		model.addAttribute("date", new DateTime().plusDays(3).toString("yyyy-MM-dd"));
		return "success";
	}

}
